package cn.fz.eventdemo01.utils;

import java.io.Console;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;

public class XxFoxUtil {
    public static String URL_REGEX = "(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]";
    public static List<String> logLevelList = Arrays.asList("", "trace", "debug", "info", "warn", "error", "fatal");

    private XxFoxUtil() {
    }

    public static void printSaToken() {
        String str = "____ ____    ___ ____ _  _ ____ _  _ \r\n[__  |__| __  |  |  | |_/  |___ |\\ | \r\n___] |  |     |  |__| | \\_ |___ | \\| \r\nhttps://sa-token.cc (v1.44.0)";
        System.out.println(str);
    }

    public static String getRandomString(int length) {
        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        StringBuilder sb = new StringBuilder();

        for(int i = 0; i < length; ++i) {
            int number = ThreadLocalRandom.current().nextInt(62);
            sb.append(str.charAt(number));
        }

        return sb.toString();
    }

    public static int getRandomNumber(int min, int max) {
        return ThreadLocalRandom.current().nextInt(min, max + 1);
    }

    public static boolean isEmpty(Object str) {
        return str == null || "".equals(str);
    }

    public static boolean isNotEmpty(Object str) {
        return !isEmpty(str);
    }

    /** @deprecated */
    @Deprecated
    public static <T> boolean isEmpty(T[] array) {
        return isEmptyArray(array);
    }

    public static <T> boolean isEmptyArray(T[] array) {
        return array == null || array.length == 0;
    }

    public static boolean isEmptyList(List<?> list) {
        return list == null || list.isEmpty();
    }

    public static boolean equals(Object a, Object b) {
        return Objects.equals(a, b);
    }

    public static boolean notEquals(Object a, Object b) {
        return !equals(a, b);
    }

    public static String getMarking28() {
        return System.currentTimeMillis() + "" + ThreadLocalRandom.current().nextInt(Integer.MAX_VALUE);
    }

    public static String formatDate(Date date) {
        return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(date);
    }

    public static String formatDate(ZonedDateTime zonedDateTime) {
        return zonedDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    public static String formatAfterDate(long ms) {
        Instant instant = Instant.ofEpochMilli(System.currentTimeMillis() + ms);
        ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, ZoneId.systemDefault());
        return formatDate(zonedDateTime);
    }

    public static List<String> searchList(Collection<String> dataList, String prefix, String keyword, int start, int size, boolean sortType) {
        if (prefix == null) {
            prefix = "";
        }

        if (keyword == null) {
            keyword = "";
        }

        List<String> list = new ArrayList<>();

        for (String key : dataList) {
            if (key.startsWith(prefix) && key.contains(keyword)) {
                list.add(key);
            }
        }

        return searchList(list, start, size, sortType);
    }

    public static List<String> searchList(List<String> list, int start, int size, boolean sortType) {
        if (!sortType) {
            Collections.reverse(list);
        }

        if (start < 0) {
            start = 0;
        }

        int end;
        if (size == -1) {
            end = list.size();
        } else {
            end = start + size;
        }

        List<String> list2 = new ArrayList();

        for(int i = start; i < end; ++i) {
            if (i >= list.size()) {
                return list2;
            }

            list2.add((String)list.get(i));
        }

        return list2;
    }

    public static boolean vagueMatch(String patt, String str) {
        if (patt == null && str == null) {
            return true;
        } else if (patt != null && str != null) {
            return !patt.contains("*") ? patt.equals(str) : vagueMatchMethod(patt, str);
        } else {
            return false;
        }
    }

    private static boolean vagueMatchMethod(String pattern, String str) {
        int m = str.length();
        int n = pattern.length();
        boolean[][] dp = new boolean[m + 1][n + 1];
        dp[0][0] = true;

        int i;
        for(i = 1; i <= n && pattern.charAt(i - 1) == '*'; ++i) {
            dp[0][i] = true;
        }

        for(i = 1; i <= m; ++i) {
            for(int j = 1; j <= n; ++j) {
                if (pattern.charAt(j - 1) != '*') {
                    if (str.charAt(i - 1) == pattern.charAt(j - 1)) {
                        dp[i][j] = dp[i - 1][j - 1];
                    }
                } else {
                    dp[i][j] = dp[i][j - 1] || dp[i - 1][j];
                }
            }
        }

        return dp[m][n];
    }

    public static boolean isWrapperType(Class<?> cs) {
        return cs == Integer.class || cs == Short.class || cs == Long.class || cs == Byte.class || cs == Float.class || cs == Double.class || cs == Boolean.class || cs == Character.class;
    }

    public static boolean isBasicType(Class<?> cs) {
        return cs.isPrimitive() || isWrapperType(cs) || cs == String.class;
    }

    public static <T> T getValueByType(Object obj, Class<T> cs) {
        if (obj != null && !obj.getClass().equals(cs)) {
            String obj2 = String.valueOf(obj);
            Object obj3;
            if (cs.equals(String.class)) {
                obj3 = obj2;
            } else if (!cs.equals(Integer.TYPE) && !cs.equals(Integer.class)) {
                if (!cs.equals(Long.TYPE) && !cs.equals(Long.class)) {
                    if (!cs.equals(Short.TYPE) && !cs.equals(Short.class)) {
                        if (!cs.equals(Byte.TYPE) && !cs.equals(Byte.class)) {
                            if (!cs.equals(Float.TYPE) && !cs.equals(Float.class)) {
                                if (!cs.equals(Double.TYPE) && !cs.equals(Double.class)) {
                                    if (!cs.equals(Boolean.TYPE) && !cs.equals(Boolean.class)) {
                                        if (!cs.equals(Character.TYPE) && !cs.equals(Character.class)) {
                                            obj3 = obj;
                                        } else {
                                            obj3 = obj2.charAt(0);
                                        }
                                    } else {
                                        obj3 = Boolean.valueOf(obj2);
                                    }
                                } else {
                                    obj3 = Double.valueOf(obj2);
                                }
                            } else {
                                obj3 = Float.valueOf(obj2);
                            }
                        } else {
                            obj3 = Byte.valueOf(obj2);
                        }
                    } else {
                        obj3 = Short.valueOf(obj2);
                    }
                } else {
                    obj3 = Long.valueOf(obj2);
                }
            } else {
                obj3 = Integer.valueOf(obj2);
            }

            return (T) obj3;
        } else {
            return (T) obj;
        }
    }

    public static <T> T mapToObject(Map<String, Object> map, Class<T> clazz) {
        if (map == null) {
            return null;
        } else if (clazz == Map.class) {
            return (T) map;
        } else {
            try {
                T obj = clazz.getDeclaredConstructor().newInstance();
                Field[] var3 = clazz.getDeclaredFields();
                int var4 = var3.length;

                for (Field field : var3) {
                    String fieldName = field.getName();
                    if (map.containsKey(fieldName)) {
                        field.setAccessible(true);
                        field.set(obj, map.get(fieldName));
                    }
                }

                return obj;
            } catch (Exception var8) {
                Exception e = var8;
                throw new RuntimeException("转换失败: " + e.getMessage(), e);
            }
        }
    }

    public static String joinParam(String url, String paramStr) {
        if (paramStr != null && !paramStr.isEmpty()) {
            if (url == null) {
                url = "";
            }

            int index = url.lastIndexOf(63);
            if (index == -1) {
                return url + '?' + paramStr;
            } else if (index == url.length() - 1) {
                return url + paramStr;
            } else if (index < url.length() - 1) {
                String separatorChar = "&";
                return url.lastIndexOf(separatorChar) != url.length() - 1 && paramStr.indexOf(separatorChar) != 0 ? url + separatorChar + paramStr : url + paramStr;
            } else {
                return url;
            }
        } else {
            return url;
        }
    }

    public static String joinParam(String url, String key, Object value) {
        return !isEmpty((Object)url) && !isEmpty((Object)key) ? joinParam(url, key + "=" + value) : url;
    }

    public static String joinSharpParam(String url, String paramStr) {
        if (paramStr != null && !paramStr.isEmpty()) {
            if (url == null) {
                url = "";
            }

            int index = url.lastIndexOf(35);
            if (index == -1) {
                return url + '#' + paramStr;
            } else if (index == url.length() - 1) {
                return url + paramStr;
            } else if (index < url.length() - 1) {
                String separatorChar = "&";
                return url.lastIndexOf(separatorChar) != url.length() - 1 && paramStr.indexOf(separatorChar) != 0 ? url + separatorChar + paramStr : url + paramStr;
            } else {
                return url;
            }
        } else {
            return url;
        }
    }

    public static String joinSharpParam(String url, String key, Object value) {
        return !isEmpty((Object)url) && !isEmpty((Object)key) ? joinSharpParam(url, key + "=" + value) : url;
    }

    public static String spliceTwoUrl(String url1, String url2) {
        if (url1 == null) {
            return url2;
        } else if (url2 == null) {
            return url1;
        } else {
            return url2.startsWith("http") ? url2 : url1 + url2;
        }
    }

    public static String arrayJoin(String[] arr) {
        if (arr == null) {
            return "";
        } else {
            StringBuilder str = new StringBuilder();

            for(int i = 0; i < arr.length; ++i) {
                str.append(arr[i]);
                if (i != arr.length - 1) {
                    str.append(",");
                }
            }

            return str.toString();
        }
    }

    public static boolean isUrl(String str) {
        return isEmpty(str) ? false : str.toLowerCase().matches(URL_REGEX);
    }

    public static String encodeUrl(String url) {
        return URLEncoder.encode(url, StandardCharsets.UTF_8);
    }


    public static List<String> convertStringToList(String str) {
        List<String> list = new ArrayList<>();
        if (isEmpty((Object)str)) {
            return list;
        } else {
            String[] arr = str.split(",");
            int var4 = arr.length;

            for (String string : arr) {
                String s = string;
                s = s.trim();
                if (!isEmpty((Object) s)) {
                    list.add(s);
                }
            }

            return list;
        }
    }

    public static String convertListToString(List<?> list) {
        if (list != null && !list.isEmpty()) {
            StringBuilder str = new StringBuilder();

            for(int i = 0; i < list.size(); ++i) {
                str.append(list.get(i));
                if (i != list.size() - 1) {
                    str.append(",");
                }
            }

            return str.toString();
        } else {
            return "";
        }
    }

    public static String[] convertStringToArray(String str) {
        List<String> list = convertStringToList(str);
        return (String[])list.toArray(new String[0]);
    }

    public static String convertArrayToString(String[] arr) {
        return arr != null && arr.length != 0 ? String.join(",", arr) : "";
    }

    public static <T> List<T> emptyList() {
        return new ArrayList<>();
    }

    public static List<String> toList(String... str) {
        return new ArrayList<>(Arrays.asList(str));
    }

    public static String[] toArray(List<String> list) {
        return (String[])list.toArray(new String[0]);
    }

    public static int translateLogLevelToInt(String level) {
        int levelInt = logLevelList.indexOf(level);
        if (levelInt <= 0 || levelInt >= logLevelList.size()) {
            levelInt = 1;
        }

        return levelInt;
    }

    public static String translateLogLevelToString(int level) {
        if (level <= 0 || level >= logLevelList.size()) {
            level = 1;
        }

        return (String)logLevelList.get(level);
    }

    public static boolean isCanColorLog() {
        Console console = System.console();
        String term = (String)System.getenv().get("TERM");
        if (console == null && term == null) {
            return true;
        } else if (console != null && term != null) {
            return true;
        } else if (console != null) {
            return false;
        } else {
            return false;
        }
    }

    public static boolean list1ContainList2AllElement(List<String> list1, List<String> list2) {
        if (list2 != null && !list2.isEmpty()) {
            if (list1 != null && !list1.isEmpty()) {
                Iterator<String> var2 = list2.iterator();

                String str;
                do {
                    if (!var2.hasNext()) {
                        return true;
                    }

                    str = (String)var2.next();
                } while(list1.contains(str));

                return false;
            } else {
                return false;
            }
        } else {
            return true;
        }
    }

    public static boolean list1ContainList2AnyElement(List<String> list1, List<String> list2) {
        if (list1 != null && !list1.isEmpty() && list2 != null && !list2.isEmpty()) {
            Iterator<String> var2 = list2.iterator();

            String str;
            do {
                if (!var2.hasNext()) {
                    return false;
                }

                str = (String)var2.next();
            } while(!list1.contains(str));

            return true;
        } else {
            return false;
        }
    }

    public static List<String> list1RemoveByList2(List<String> list1, List<String> list2) {
        if (list1 == null) {
            return null;
        } else if (!list1.isEmpty() && list2 != null && !list2.isEmpty()) {
            List<String> listX = new ArrayList<>(list1);

            for (String str : list2) {
                listX.remove(str);
            }

            return listX;
        } else {
            return new ArrayList<>(list1);
        }
    }

    public static boolean hasNonPrintableASCII(String str) {
        if (str != null) {
            for (int i = 0; i < str.length(); ++i) {
                char c = str.charAt(i);
                if (c <= 31 || c == 127) {
                    return true;
                }
            }

        }
        return false;
    }

    public static String valueToString(Object value) {
        return value == null ? "" : value.toString();
    }
}
